/*
 * GWT Portlets Framework (http://code.google.com/p/gwtportlets/)
 * Copyright 2010 Business Systems Group (Africa)
 *
 * This file is part of GWT Portlets.
 *
 * GWT Portlets is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GWT Portlets is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GWT Portlets.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.gwtportlets.portlet.smartgwt.client;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSProtocol;

/**
 * The data source which must be used for data integration in Portlets.
 *
 * This implementation was made possible with the use of the GWT RPC example posted by
 * Aleksandras Novikovas in the SmartGwt forums.
 * @see http://forums.smartclient.com/showthread.php?t=4814
 *
 * @author Carl Crous
 */
public class SmartPortletDataSource extends DataSource {
    private SmartPortlet portlet;

    public SmartPortletDataSource(SmartPortlet portlet) {
        this.portlet = portlet;
        setDataProtocol(DSProtocol.CLIENTCUSTOM);
        setDataFormat(DSDataFormat.CUSTOM);
        setClientOnly(false);
    }

    private SmartPortletDataSource() {
    }

    public SmartPortlet getPortlet() {
        return portlet;
    }

    public void setPortlet(SmartPortlet portlet) {
        this.portlet = portlet;
    }

    /**
     * The API hook into Smart Gwt. This is where we use the Portlet framework for data integration.
     * @param request The request generated by the Smart Gwt client.
     * @return The data in the request.
     */
    @Override
    protected Object transformRequest(DSRequest request) {
        portlet.addRequest(request);
        SmartPortletFactory wf = (SmartPortletFactory)portlet.createWidgetFactory();
        wf.setRequestId(request.getRequestId());
        wf.setDataSourceId(portlet.getBaseDataSourceId(getID()));
        wf.setComponentId(request.getComponentId());

        switch (request.getOperationType()) {
            case FETCH:
                wf.executeFetch(this, request);
                break;
            case ADD:
                wf.executeAdd(this, request);
                break;
            case UPDATE:
                wf.executeUpdate(this, request);
                break;
            case REMOVE:
                wf.executeRemove(this, request);
                break;
            default:
                // Operation not implemented.
                wf.operationType = 0;
                break;
        }
        portlet.refresh(wf, null, true);
        return request.getData();
    }
}
